//
//  FriendSetViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14-10-9.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "FriendSetViewController.h"
@interface FriendSetViewController ()
{
//    BOOL isTeacherButtonOn;
//    BOOL isStudentButtonOn;
}

@property (strong, nonatomic) UIButton * submitBtn;
@property(strong, nonatomic) UISwitch *allowTeacherSwitchBtn;
@property(strong, nonatomic) UISwitch *allowStudentSwitchBtn;

@end

@implementation FriendSetViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:236/255.0f green:235/255.0f blue:243/255.0f alpha:1];
    [self addTitleBarWithBackBtn:@"好友设置" needReturn:YES];
    NSString * friendrange = [DE getGlobalVar:LOGIN_USER_FRIEDNRANG];
    //去掉背景
    UIView * teacherBackView = [[UIView alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW)+20, SCREEN_WIDTH, 40)];
    teacherBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:teacherBackView];
    UILabel * allowTeacherLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 9, 120, 21)];
    allowTeacherLabel.text = @"允许老师添加";
    [teacherBackView addSubview:allowTeacherLabel];
    _allowTeacherSwitchBtn = [[UISwitch alloc] initWithFrame:CGRectMake((SCREEN_WIDTH)-80, 5, 20, 20)];
    [_allowTeacherSwitchBtn addTarget:self action:nil forControlEvents:UIControlEventValueChanged];
    [teacherBackView addSubview:_allowTeacherSwitchBtn];
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:236/255.0f green:235/255.0f blue:243/255.0f alpha:1];
    [teacherBackView addSubview:lineView];
    UIView * studentBackView = [[UIView alloc] initWithFrame:CGRectMake(0, teacherBackView.frame.size.height+teacherBackView.frame.origin.y, SCREEN_WIDTH, 40)];
    studentBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:studentBackView];
    UILabel * allowStudentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 9, 120, 21)];
    allowStudentLabel.text = @"允许学生添加";
    [studentBackView addSubview:allowStudentLabel];
    _allowStudentSwitchBtn = [[UISwitch alloc] initWithFrame:CGRectMake((SCREEN_WIDTH)-80, 5, 20, 20)];
    [_allowStudentSwitchBtn addTarget:self action:nil forControlEvents:UIControlEventValueChanged];
    [studentBackView addSubview:_allowStudentSwitchBtn];
    if (friendrange == nil) {
        [_allowTeacherSwitchBtn setOn:NO];
        [_allowStudentSwitchBtn setOn:NO];
     }else if ([friendrange isEqualToString:@"T"])
    {
        [_allowTeacherSwitchBtn setOn:YES];
        [_allowStudentSwitchBtn setOn:NO];
    }else if ([friendrange isEqualToString:@"S"])
    {
        [_allowTeacherSwitchBtn setOn:NO];
        [_allowStudentSwitchBtn setOn:YES];
    }else if ([friendrange isEqualToString:@"TS"])
    {
        [_allowTeacherSwitchBtn setOn:YES];
        [_allowStudentSwitchBtn setOn:YES];
    }else
    {
        [_allowTeacherSwitchBtn setOn:NO];
        [_allowStudentSwitchBtn setOn:NO];
    }
    UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
    img=[img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    _submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, _allowStudentSwitchBtn.frame.origin.y+200, SCREEN_WIDTH-20, 35)];
    [_submitBtn addTarget:self action:@selector(submitBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_submitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_submitBtn setBackgroundImage:img forState:UIControlStateNormal];
    [self.view addSubview:_submitBtn];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)submitBtn:(id)sender
{
     NSString * friendRange =@"";

    bool isTeacherButtonOn = [_allowTeacherSwitchBtn isOn];
    bool isStudentButtonOn = [_allowStudentSwitchBtn isOn];

    
    if (isTeacherButtonOn && isStudentButtonOn) {
        friendRange = @"TS";
    }else if (isTeacherButtonOn && !isStudentButtonOn)
    {
        friendRange = @"T";
    }else if (!isTeacherButtonOn && isStudentButtonOn)
    {
        friendRange = @"S";
    }else
    {
        friendRange = @"";
    }
    NSDictionary *params = @{@"friendRange":friendRange};
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"updateFriendConfig" params:params callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success && [data objectForKey:@"result"])
        {
            [DE setGlobalVar:LOGIN_USER_FRIEDNRANG value:friendRange];
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"设置成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
        }else
        {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"设置失败!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
        }
    }];

}


@end
